 /* 
 * File:   cAnimatedCharacterViewer.cpp
 * Author: josep
 * 
 * Created on 11 / desembre / 2010, 00:31
 */


#include "cAnimatedVisibleObject.h"
#include "utility.h"

#define TAG_STATE "state:"
#define TAG_STATE_SIZE 6
#define TAG_IMAGES "images:"
#define TAG_IMAGES_SIZE 7
#define TAG_POS "pos:"
#define TAG_POS_SIZE 4
#define TAG_TILECOORDX "tilecoordx:"
#define TAG_TILECOORDX_SIZE 11
#define TAG_TILECOORDY "tilecoordy:"
#define TAG_TILECOORDY_SIZE 11
#define TAG_HOR "Hor"
#define TAG_HOR_SIZE 3

cAnimatedVisibleObject::cAnimatedVisibleObject() {
    SetFrameState(0);
}

cAnimatedVisibleObject::cAnimatedVisibleObject(const cAnimatedVisibleObject& orig):
                                                 cPhysicalVisibleObject(orig),
                                                 cAbstractAnimatedObject(orig) {
}

cAnimatedVisibleObject::~cAnimatedVisibleObject() {
}

void cAnimatedVisibleObject::Draw(){
    cAbstractAnimatedObject::Draw(GetPosX(), GetPosY(), GetTexture(),
                                        GetTextureWidth(), GetTextureHeight());
}

void cAnimatedVisibleObject::InitFromIniFile(const char* pName, cData *Data,
                                    int posX, int posY, int width, int height){
        string fileName;
        string buffer;
        string iniFileName;
        ///stringstream  iniFileName;

        //if(strcasestr(pName, ".ini")==NULL){
        if(strstr(pName, ".ini")==NULL){
            iniFileName = concatToString("ss", pName, ".ini"); // << pName << ".ini";
        }else{
            iniFileName = pName; //<< pName;
        }

        ifstream inFileStream(iniFileName.c_str());
        if(!inFileStream.is_open()) {
              throw PECVJ2D_ERROR_FILE_NOT_FOUND;
        }
        while(getline(inFileStream, buffer)) {
            if(buffer[0]=='f'){
                setFileImage(buffer, fileName);
            }else if(buffer[0]=='n' && buffer[1]=='T'){
                setNTiles(buffer);
            }else if(buffer[0]=='n' && buffer[1]=='F'){
                setNFrameStates(buffer);
            }else if(buffer[0]=='n' && buffer[1]=='I'){
                setNImagesFromState(buffer);
            }else if(buffer[0]=='i'){
                setImageSequencesVector(buffer);
            }else if(buffer[0]=='t' && buffer[1]=='i'){
                int tileSize;
                setTileSize(buffer, tileSize);
                width=tileSize;
                height=tileSize;

            }
        }
        inFileStream.close();

        cTexture * img;
        if((img = Data->loadImage(fileName, GL_RGBA))!=NULL){
            SetFrameSeq(0);
            SetFrameRate(20);
    	    SetFrameState(0);
            
            Init(img, posX, posY, width, height);
        }
}


/*PRIVATE*/
void cAnimatedVisibleObject::setFileImage(string iniValueLine, string &fileName){
    int rpos = iniValueLine.find("\r");
    int lpos = iniValueLine.find(CHAR_EQUAL)+1;
    fileName = iniValueLine.substr(lpos, rpos-lpos);
}

void cAnimatedVisibleObject::setTileSize(string iniValueLine, int &tileSize){
    int rpos = iniValueLine.find("\r");
    int lpos = iniValueLine.find(CHAR_EQUAL)+1;
    tileSize = stringToint(iniValueLine.substr(lpos, rpos-lpos));
}

void cAnimatedVisibleObject::setNTiles(string iniValueLine){
    int nTiles;
    int rpos = iniValueLine.find("\r");
    int lpos = iniValueLine.find(CHAR_EQUAL)+1;
    stringstream ss;
    if(iniValueLine.find(TAG_HOR)<iniValueLine.find(CHAR_EQUAL)){
        nTiles= stringToint(iniValueLine.substr(lpos, rpos-lpos));
        SetNTilesHor(nTiles);
    }else{
        nTiles= stringToint(iniValueLine.substr(lpos, rpos-lpos));
        SetNTilesVer(nTiles);
    }
}

void cAnimatedVisibleObject::setNFrameStates(string iniValueLine){
    int frameState;
    int rpos = iniValueLine.find("\r");
    int lpos = iniValueLine.find(CHAR_EQUAL)+1;
    frameState= stringToint(iniValueLine.substr(lpos, rpos-lpos));
    SetFrameState(frameState);
    GetImageSequencesVector()->resize(frameState);
}
void cAnimatedVisibleObject::setNImagesFromState(string iniValueLine){
    int frameState, nImagesFromState, pos;
    int rpos = iniValueLine.find("\r");

    pos = iniValueLine.find(TAG_STATE);
    pos+=TAG_STATE_SIZE;
    frameState = stringToint(iniValueLine.substr(pos, iniValueLine.find(pos, CHAR_COMMA)-pos));
    pos=iniValueLine.find(TAG_IMAGES);
    pos +=TAG_IMAGES_SIZE;//images:
    nImagesFromState = stringToint(iniValueLine.substr(pos, iniValueLine.find(pos, CHAR_COMMA)-pos));
    (*(GetImageSequencesVector()))[frameState].resize(nImagesFromState);
    //imageSequencesVector[frameState].resize(nImagesFromState);
    for(int i=0; i<nImagesFromState; i++){
        (*(GetImageSequencesVector()))[frameState][i].resize(2);
        //imageSequencesVector[frameState][i].resize(2);
    }

}

void cAnimatedVisibleObject::setImageSequencesVector(string iniValueLine){
    int posSubs, frameState, pos, tilecoordx, tilecoordy;
    posSubs = iniValueLine.find(TAG_STATE) ;
    posSubs+=TAG_STATE_SIZE;
    frameState = stringToint(iniValueLine.substr(posSubs, iniValueLine.find(posSubs, CHAR_COMMA)-posSubs));
    posSubs = iniValueLine.find(TAG_POS);
    posSubs+=TAG_POS_SIZE;
    pos = stringToint(iniValueLine.substr(posSubs, iniValueLine.find(posSubs, CHAR_COMMA)-posSubs));
    posSubs=iniValueLine.find(TAG_TILECOORDX);
    posSubs+=TAG_TILECOORDX_SIZE;
    tilecoordx=stringToint(iniValueLine.substr(posSubs, iniValueLine.find(posSubs, CHAR_COMMA)-posSubs));
    posSubs=iniValueLine.find(TAG_TILECOORDY);
    posSubs+=TAG_TILECOORDY_SIZE;
    tilecoordy = stringToint(iniValueLine.substr(posSubs, iniValueLine.find(posSubs, CHAR_COMMA)-posSubs));

    (*(GetImageSequencesVector()))[frameState][pos][0]=tilecoordx;
    //imageSequencesVector[frameState][pos][0]=tilecoordx;
    (*(GetImageSequencesVector()))[frameState][pos][1]=tilecoordy;
    //imageSequencesVector[frameState][pos][1]=tilecoordy;
}
